// @Author yy 2024/8/21  学生回答完毕提交结果
package conference_handler

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"peilian-api/app/global/bind"
	"peilian-api/app/global/databases/tables"
	"peilian-api/app/global/errors"
	"peilian-api/app/global/variable"
	"peilian-api/app/model"
	"peilian-api/utils/format"
	"time"
)

//StudentReply
/**
*@author: yy
*@description: 学生回答完毕提交结果
*@date :2024/8/22 上午9:09
**/
func StudentReply(ctx *gin.Context) {
	var studentReplyDO tables.ConferenceStudentReply
	if err := bind.ShouldBinding(ctx, &studentReplyDO); err != nil {
		format.NewResponseJson(ctx).Err(errors.PublicErrorCode, errors.ParamBindError)
		return
	}
	// 停止云端录制 获取文件地址
	fileName, errRes := StopRecordConference(studentReplyDO.Cname, studentReplyDO.Uid, studentReplyDO.Resourceid, studentReplyDO.Sid)
	if errRes != nil {
		// 停止云端录制失败
		format.NewResponseJson(ctx).Err(errRes.Code, errRes.Err.Error())
	}
	// 拼接文件地址
	fileUrl := variable.Config.AliOss.OssUrl + fileName
	studentReplyDO.ReplyVideoUrl = fileUrl
	err := variable.DB.MySQL.Create(&studentReplyDO).Error
	if err != nil {
		format.NewResponseJson(ctx).Err(500, fmt.Errorf("学生回答完毕提交结果失败").Error())
	}
	format.NewResponseJson(ctx).SuccessObj("操作成功")

}

/**
*@author: yy
*@description: 学生结束讨论会
  1.修改会议状态
  2.生成session_id
  3.
*@date :2024/8/22 下午2:47
**/
func StudentFinish(ctx *gin.Context) {
	var finishDTO model.FinishConferenceDTO
	if err := bind.ShouldBinding(ctx, &finishDTO); err != nil {
		format.NewResponseJson(ctx).Err(errors.PublicErrorCode, errors.ParamBindError)
		return
	}
	// 修改会议状态，查询会议信息
	var conference tables.Conference
	variable.DB.MySQL.Find(&conference, finishDTO.ConferenceId)
	if conference.ID == 0 {
		format.NewResponseJson(ctx).Err(500, "未查询到会议信息")
		return
	}
	// 查询学生报名信息
	var studentApply tables.ConferenceApply
	variable.DB.MySQL.Find(&studentApply, finishDTO.StudentId)
	if conference.ConferenceStatus == "0" {
		conference.ConferenceStatus = "1"
		conference.EndTime = time.Now()
		// 更新会议状态
		variable.DB.MySQL.Save(&conference)
	}
	career, err := tables.GetCareerInfoById(conference.CareerId)
	if err != nil || career.ID == 0 {
		format.NewResponseJson(ctx).Err(500, "该职业面试不存在")
		return
	}
	var session tables.Session
	// 根据用户获取用户信息
	u, err := tables.GetUserInfoById(int(studentApply.StudentId))
	if err != nil {
		format.NewResponseJson(ctx).Err(500, "fetch user fail")
		return
	}
	if u.ID == 0 {
		format.NewResponseJson(ctx).Err(500, "user is not exsit")
		return
	}
	session.UserID = studentApply.StudentId
	session.CareerID = conference.CareerId
	session.School = u.School
	session.Academy = u.Academy
	session.Profession = u.Profession
	session.Education = u.Education
	session.Grade = u.Grade
	session.UserName = u.Name
	session.UserPhone = u.Phone
	session.CareerName = career.Name
	session.TrainAt = time.Now()
	session.Type = u.Type
	session.Sno = u.Sno
	session.IsSync = u.IsSync
	d := variable.DB.MySQL.Create(&session)
	if d.Error != nil {
		format.NewResponseJson(ctx).Err(500, "生成面试场次失败")
		return
	}
	studentApply.SessionId = session.ID
	variable.DB.MySQL.Save(&studentApply)
	format.NewResponseJson(ctx).SuccessObj("操作成功")
}
